package com.easy.facilitaterecycleadapter.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

/**
 * Created by Sigthp-P-Department on 2018/5/4.
 */

public class CustomLineLayoutDecoration extends RecyclerView.ItemDecoration {

    private Drawable mDrawable;

    public CustomLineLayoutDecoration(Drawable drawable){
        mDrawable=drawable;
    }
    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        Rect rect=new Rect();
        rect.left=parent.getPaddingLeft();
        rect.right=parent.getWidth()-parent.getPaddingRight();
        for(int x=0;x<parent.getChildCount();x++){
            View childView = parent.getChildAt(x);
            rect.top=childView.getBottom();
            rect.bottom=rect.top+mDrawable.getIntrinsicHeight();
            mDrawable.setBounds(rect);
            mDrawable.draw(c);
        }

    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = ((RecyclerView.LayoutParams) view.getLayoutParams()).getViewLayoutPosition();
        if((position+1)<=state.getItemCount()-1){
            outRect.bottom=outRect.bottom+mDrawable.getIntrinsicHeight();
        }

    }
}
